#include "time.h"

static const char* G_MONTH_LIST[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

int change(char *t, int x, int length)
{ //将字符转化为数字
	char s[10] = "";
	strncpy(s, t + x, length);
	return atoi(s);
}

int LastTime(char *file_src, char *file_dest)
{ //比较两个文件最后访问时间，file1最新返回0，file2最新返回1
	struct stat stat_src, stat_dest;
	char time_src[50], time_dest[50];
	char s1[10], s2[10];
	int year1 = 0, month1 = 0, day1 = 0, time1 = 0;
	int year2 = 0, month2 = 0, day2 = 0, time2 = 0;
	//读取文件stat
	stat(file_src, &stat_src);
	stat(file_dest, &stat_dest);
	strcpy(time_src, ctime(&stat_src.st_mtime));
	strcpy(time_dest, ctime(&stat_dest.st_mtime));
	//获取月份
	strncpy(s1, time_src + 4, 3);
	strncpy(s2, time_dest + 4, 3);
	for (int i = 0; i < 12; i++)
	{
		if (strncmp(G_MONTH_LIST[i], s1, 3) == 0)
		{
			month1 = i + 1;
		}
		if (strncmp(G_MONTH_LIST[i], s2, 3) == 0)
		{
			month2 = i + 1;
		}
	}
	//	printf("month %d %d\n",month1,month2);
	//获取天数
	day1 = change(time_src, 8, 2);
	day2 = change(time_dest, 8, 2);
	//	printf("day %d %d\n",day1,day2);
	//获取年份
	year1 = change(time_src, 20, 4);
	year2 = change(time_dest, 20, 4);
	//	printf("year %d %d\n",year1,year2);
	//获取当天时间
	time1 = change(time_src, 11, 2) * 3600 + change(time_src, 14, 2) * 60 + change(time_src, 17, 2);
	time2 = change(time_dest, 11, 2) * 3600 + change(time_dest, 14, 2) * 60 + change(time_dest, 17, 2);
	//	printf("time1 %d %d %d  %d\n",change(t1,11,2),change(t1,14,2),change(t1,17,2),time1);
	//	printf("time2 %d %d %d  %d\n",change(t2,11,2),change(t2,14,2),change(t2,17,2),time2);
	if (year1 > year2)
		return 0;
	else if (year1 == year2)
	{
		if (month1 > month2)
			return 0;
		else if (month1 == month2)
		{
			if (day1 > day2)
				return 0;
			else if (day1 == day2)
			{
				if (time1 > time2)
				{
					return 0;
				}
			}
		}
	}
	return 1;
}

